package com.duang.framework.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public abstract class IntrospectionUtils {

	public static String getMethodSignKeyInClass(Method method) {
		StringBuilder sb = new StringBuilder();
		sb.append(method.getName());
		for (Class<?> clazz : method.getParameterTypes())
			sb.append(clazz.getName());
		return sb.toString();
	}

	public static String getMethodSignKey(Method method) {
		StringBuilder sb = new StringBuilder();
		sb.append(method.getModifiers());
		sb.append(method.getReturnType().getName());
		sb.append(method.getName());
		for (Class<?> clazz : method.getParameterTypes())
			sb.append(clazz.getName());
		return sb.toString();
	}

	public static List<Method> getMethods(Class<?> clazz,
			boolean recursiveParent) {
		if (!recursiveParent)
			return Arrays.asList(clazz.getMethods());

		List<Method> methodList = new ArrayList<Method>();
		for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass()) {
			for (Method method : c.getDeclaredMethods()) {
				methodList.add(method);
			}
		}
		return methodList;

	}

	public static List<Field> getFields(Class<?> clazz, boolean recursiveParent) {
		if (!recursiveParent)
			return Arrays.asList(clazz.getDeclaredFields());
		List<Field> fieldList = new ArrayList<Field>();
		for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass()) {
			for (Field field : c.getDeclaredFields()) {
				fieldList.add(field);
			}
		}
		return fieldList;
	}

}
